package com.jialiang.base.admin.account.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jialiang.auth.api.constants.PageData;
import com.jialiang.base.admin.account.domain.pojo.entity.Role;
import com.jialiang.base.admin.account.domain.pojo.entity.User;
import com.jialiang.base.admin.account.domain.pojo.vo.RoleListItemVO;
import com.jialiang.base.admin.account.domain.service.IRoleService;
import com.jialiang.base.admin.account.domain.service.IUserRoleService;
import com.jialiang.base.admin.account.domain.service.IUserService;
import com.jialiang.common.model.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 处理角色相关请求的控制器
 *
 * @author jialiang
 * @version 3.0
 */
@Slf4j
@RestController
@RequestMapping("/roles")
@Validated
@Api(tags = "2. 角色管理")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IUserService userService;

    public RoleController() {
        log.debug("创建控制器类对象：RoleController");
    }

    @GetMapping("")
//    @PreAuthorize("hasAuthority('/account/roles/query')")
    @ApiOperation("查询角色列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    public JsonObject<?> list(Integer page, String queryType) {
        log.debug("开始处理【查询角色列表】的请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<RoleListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = roleService.list(1, Integer.MAX_VALUE);
        } else {
            pageData = roleService.list(pageNum);
        }
        return JsonObject.success(pageData);
    }

    @GetMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/account/roles/delete')")
    @ApiOperation("解绑该用户绑定的所有角色")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "long")
    })
    public JsonObject<?> delete(@PathVariable @Range(min = 1, message = "请提交合法的角色ID值！") Long id) {
        log.debug("开始处理【根据ID删除角色】的请求，参数：{}", id);
        userService.deleteByUser(id);
        return JsonObject.success();
    }

    @GetMapping("/{id:[0-9]+}/users")
    @PreAuthorize("hasAuthority('/account/roles/query')")
    @ApiOperation("根据角色ID查询该角色绑定的所有用户")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "long")
    })
    public JsonObject<List<User>> listUsers(@PathVariable @Range(min = 1, message = "请提交合法的角色ID值！") Long id) {
        log.debug("开始处理【根据角色ID查询该角色绑定的所有用户】的请求，参数：{}", id);
        return JsonObject.success(userRoleService.listUsersByRoleId(id));
    }

    //查询用户的角色列表
    @ApiOperation("根据用户ID查询用户的角色列表")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    @GetMapping("/{id:[0-9]+}/roles")
    @PreAuthorize("hasAuthority('/account/roles/query')")
    public JsonObject<List<Role>> listRolesByUserId(@PathVariable @Range(min = 1, message ="请提交合法的用户ID值！") Long id) {
        log.debug("开始处理【根据用户ID查询用户的角色列表】的请求，参数：{}", id);
        return JsonObject.success(userRoleService.listRolesIdByUserId(id));
    }


}
